package com.dao.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.BasicQuery;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;

import com.bean.LineTable;
import com.bean.UserList;
import com.bean.UserLogin;
import com.dao.UserListDao;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.util.Pager;

@Component
public class UserListDaoImpl implements UserListDao{

	@Autowired
	private MongoTemplate mongoTemplate;

	@Override
	public UserList selectByUserName(String id,String userId) {
		UserList data=mongoTemplate.findById(id, UserList.class);
		UserLogin userLogin=null;
		if(StringUtils.isNotEmpty(userId)){
			userLogin=mongoTemplate.findById(userId, UserLogin.class);
		}
		if(userLogin!=null){
			long countFollow=mongoTemplate.count(new Query(Criteria.where("_id")
					.is(userId).and("follow.followId").is(data.getAid())), UserLogin.class);
			if(countFollow>0){
				data.setMutual_concern("true");
			}else{
				data.setMutual_concern("false");
			}
		}else{
			data.setMutual_concern("false");
		}
		return data;
	}

	@Override
	public Pager<UserList> selectByOfficial(Integer curPage, Integer pageSize,String userId) {
		Pager<UserList> pager=new Pager<UserList>();
		if(curPage!=null)
			pager.setCurPage(curPage);
		if(pageSize!=null)
			pager.setPageSize(pageSize);
		Criteria criteria=Criteria.where("verify").is("2");
		List<UserList> data=mongoTemplate.find(
				new Query(criteria).with(new Sort(Sort.Direction.DESC,"followed")).skip(pager.getStart()).
					limit(pager.getPageSize()), 
					UserList.class);
		UserLogin userLogin=null;
		if(StringUtils.isNotEmpty(userId)){
			userLogin=mongoTemplate.findById(userId, UserLogin.class);
		}
		for(UserList dat:data){
			if(userLogin!=null){
				long countFollow=mongoTemplate.count(new Query(Criteria.where("_id")
						.is(userId).and("follow.followId").is(dat.getAid())), UserLogin.class);
				if(countFollow>0){
					dat.setMutual_concern("true");
				}else{
					dat.setMutual_concern("false");
				}
			}else{
				dat.setMutual_concern("false");
			}
		}
		pager.setData(data);
		long count=mongoTemplate.count(new Query(criteria), UserList.class);
		pager.setTotalRow(Integer.valueOf(String.valueOf(count)));
		return pager;
	}

	@Override
	public Pager<UserList> selectByStar(Integer curPage, Integer pageSize,String userId) {
		Pager<UserList> pager=new Pager<UserList>();
		if(curPage!=null)
			pager.setCurPage(curPage);
		if(pageSize!=null)
			pager.setPageSize(pageSize);
		Criteria criteria=Criteria.where("verify").is("1");
		List<UserList> data=mongoTemplate.find(
				new Query(criteria).with(new Sort(Sort.Direction.DESC,"followed")).skip(pager.getStart()).
					limit(pager.getPageSize()), 
					UserList.class);
		UserLogin userLogin=null;
		if(StringUtils.isNotEmpty(userId)){
			userLogin=mongoTemplate.findById(userId, UserLogin.class);
		}
		for(UserList dat:data){
			if(userLogin!=null){
				long countFollow=mongoTemplate.count(new Query(Criteria.where("_id")
						.is(userId).and("follow.followId").is(dat.getAid())), UserLogin.class);
				if(countFollow>0){
					dat.setMutual_concern("true");
				}else{
					dat.setMutual_concern("false");
				}
			}else{
				dat.setMutual_concern("false");
			}
		}
		pager.setData(data);
		long count=mongoTemplate.count(new Query(criteria), UserList.class);
		pager.setTotalRow(Integer.valueOf(String.valueOf(count)));
		return pager;
	}
	@Override
	public Pager<UserList> selectByHost(Integer curPage, Integer pageSize,String userId) {
		Pager<UserList> pager=new Pager<UserList>();
		if(curPage!=null)
			pager.setCurPage(curPage);
		if(pageSize!=null)
			pager.setPageSize(pageSize);
		List<UserList> data=mongoTemplate.find(
				new Query().skip(pager.getStart()).
					limit(pager.getPageSize()).with(new Sort(Sort.Direction.DESC,"followed")), 
					UserList.class);
		UserLogin userLogin=null;
		if(StringUtils.isNotEmpty(userId)){
			userLogin=mongoTemplate.findById(userId, UserLogin.class);
		}
		for(UserList dat:data){
			if(userLogin!=null){
				long countFollow=mongoTemplate.count(new Query(Criteria.where("_id")
						.is(userId).and("follow.followId").is(dat.getAid())), UserLogin.class);
				if(countFollow>0){
					dat.setMutual_concern("true");
				}else{
					dat.setMutual_concern("false");
				}
			}else{
				dat.setMutual_concern("false");
			}
		}
		pager.setData(data);
		long count=mongoTemplate.count(new Query(), UserList.class);
		pager.setTotalRow(Integer.valueOf(String.valueOf(count)));
		return pager;
	}
	
	@Override
	public Pager<UserList> selectFindLine(Integer curPage,Integer pageSize){
		Pager<UserList> pager=new Pager<UserList>();
		if(curPage!=null)
			pager.setCurPage(curPage);
		if(pageSize!=null)
			pager.setPageSize(pageSize);
		DBObject dbObject = new BasicDBObject();
		dbObject.put("isSup", true);
		DBObject fieldObject = new BasicDBObject();
		fieldObject.put("_id", true);
		fieldObject.put("plains", true);
		fieldObject.put("lineImg", true);
		fieldObject.put("username", true);
		fieldObject.put("img", true);
		fieldObject.put("title", true);
		Query query = new BasicQuery(dbObject, fieldObject);
		
		List<UserList> ulist=mongoTemplate.find(query.addCriteria(Criteria.where("plains").exists(true)).skip(pager.getStart()).limit(pager.getPageSize()),UserList.class);
		for(UserList user:ulist){
			if(user.getPlains()!=null&&user.getPlains().length>0){
				user.setLineCount(user.getPlains().length);
				LineTable line=mongoTemplate.findById(user.getPlains()[0], LineTable.class);
				if(line!=null){
					user.setLikeCount(line.getZanNum2());
				}
			}
		}
		pager.setData(ulist);
		long count=mongoTemplate.count(query.addCriteria(Criteria.where("plains").exists(true)), UserList.class);
		pager.setTotalRow(Integer.valueOf(String.valueOf(count)));
		return pager;
	}
}
